<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

use think\Db;

include_once __DIR__. "/common/http.php";


function toResful($code,$data,$msg=''){
    return  [
        'code'=>$code,
        'data'=>$data,
        'msg'=>$msg
    ];
}

//返回今天的开始时间
function today(){
    $start_time=strtotime(date("Y-m-d",time()));
    $end_time=$start_time+60*60*24;
    return [$start_time,$end_time];
}

function yesterday(){
    $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
    $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
    return [$beginYesterday,$endYesterday];
}

//本月的开始时间和结束时间
function month(){
    $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
    $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
    return [$beginThismonth,$endThismonth];
}

//获取文件后缀名

 function getSuffix($name){
     return '.'.substr(strrchr($name, '.'), 1);
 }

//添加日志
function setLog($key,$value){
    try{
        $res= Db::table('logs')->insert([
            'key'=>$key,
            'value'=>$value
        ]);
    }catch (\Exception $e){
        $res= Db::table('logs')->insert([
            'key'=>'【setLog_err】'.$key,
            'value'=>$e->getMessage()
        ]);
    }

}

//c获取url中的参数
function getUrlQuery($url){
    $url2 = parse_url($url);

    return convertUrlQuery($url2['query']);
}

function convertUrlQuery($query)
{
    $queryParts = explode('&', $query);

    $params = array();
    foreach ($queryParts as $param) {
        $item = explode('=', $param);
        try{
            $params[$item[0]] = $item[1];
        }catch (\Exception $e){

        }
    }

    return $params;
}

